﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MuaDeal.DTO;
namespace MuaDeal
{
    public partial class XemGioHang : Form
    {
        public XemGioHang()
        {
            InitializeComponent();
        }
        gioHangDTO gh = new gioHangDTO();

        internal gioHangDTO Gh
        {
            get { return gh; }
            set { gh = value; }
        }
        private void XemGioHang_Load(object sender, EventArgs e)
        {
             try
            {
                Datahelper helper = new Datahelper();
                helper.Open();
                SqlCommand comman = new SqlCommand();
                comman.CommandText = "sp_LayChiTietGioHangTheoMaGH";
                comman.CommandType = CommandType.StoredProcedure;
                SqlParameter bien1 = comman.Parameters.Add("@magh", SqlDbType.Char,30);
                bien1.Direction = ParameterDirection.Input;
                bien1.Value = gh.MaGioHang;
                comman.Connection = helper.Connection;
              
                SqlDataReader reader = comman.ExecuteReader();
                listView1.Items.Clear();
                string masp = "";
                while (reader.Read())
                {
                    ListViewItem item = new ListViewItem();

                    item.Text = reader[2].ToString();
                    item.SubItems.Add(reader[4].ToString());
                    item.SubItems.Add(reader[3].ToString());//so luong
                    item.SubItems.Add(reader[5].ToString());//don gia
                    int sl = int.Parse(reader[3].ToString());
                    float gia = float.Parse(reader[5].ToString());
                    double thanhtien = gia * sl;
                    item.SubItems.Add(thanhtien.ToString());//don gia
                    listView1.Items.Add(item);
                }
                reader.Close();
                helper.Close();

                //load lên các label
                lbtenkh1.Text = gh.TenUser;
                lbngaymua1.Text = gh.NgayDatHang.ToString();
                lbtinhtrang1.Text = gh.MaTinhTrangGioHang.ToString();
                
                lbmagh1.Text = gh.MaGioHang.ToString();
                lbtongtien1.Text = double.Parse(gh.TongTien.ToString()).ToString();
            }catch(System.Exception ex)
            {
                MessageBox.Show("Lỗi " + ex.Message);
            }
                
        }

        private void lbmagh1_Click(object sender, EventArgs e)
        {

        }

        private void rdbtnsai_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnxemlai_Click(object sender, EventArgs e)
        {
            if(rdbtnreaduncommitted.Checked==true)
            {
                try
                {
                    Datahelper helper = new Datahelper();
                    helper.Open();
                    SqlCommand comman = new SqlCommand();
                    comman.CommandText = "sp_LayChiTietGioHangTheoMaGH_Readuncommitted";
                    comman.CommandType = CommandType.StoredProcedure;
                    SqlParameter bien1 = comman.Parameters.Add("@magh", SqlDbType.Char, 30);
                    bien1.Direction = ParameterDirection.Input;
                    bien1.Value = gh.MaGioHang;
                    comman.Connection = helper.Connection;

                    SqlDataReader reader = comman.ExecuteReader();
                    listView1.Items.Clear();
                    string masp = "";
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem();

                        item.Text = reader[2].ToString();
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[3].ToString());//so luong
                        item.SubItems.Add(reader[5].ToString());//don gia
                        int sl = int.Parse(reader[3].ToString());
                        float gia = float.Parse(reader[5].ToString());
                        double thanhtien = gia * sl;
                        item.SubItems.Add(thanhtien.ToString());//don gia
                        listView1.Items.Add(item);
                    }
                    reader.Close();
                    helper.Close();

                    //load lên các label
                    lbtenkh1.Text = gh.TenUser;
                    lbngaymua1.Text = gh.NgayDatHang.ToString();
                    lbtinhtrang1.Text = gh.MaTinhTrangGioHang.ToString();

                    lbmagh1.Text = gh.MaGioHang.ToString();
                    lbtongtien1.Text = double.Parse(gh.TongTien.ToString()).ToString();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Lỗi " + ex.Message);
                }
            
            }
            else if(rdbtnrepeatableread.Checked==true)
            {
                try
                {
                    Datahelper helper = new Datahelper();
                    helper.Open();
                    SqlCommand comman = new SqlCommand();
                    comman.CommandText = "sp_LayChiTietGioHangTheoMaGH_RepeatableRead";
                    comman.CommandType = CommandType.StoredProcedure;
                    SqlParameter bien1 = comman.Parameters.Add("@magh", SqlDbType.Char, 30);
                    bien1.Direction = ParameterDirection.Input;
                    bien1.Value = gh.MaGioHang;
                    comman.Connection = helper.Connection;

                    SqlDataReader reader = comman.ExecuteReader();
                    listView1.Items.Clear();
                    string masp = "";
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem();

                        item.Text = reader[2].ToString();
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[3].ToString());//so luong
                        item.SubItems.Add(reader[5].ToString());//don gia
                        int sl = int.Parse(reader[3].ToString());
                        float gia = float.Parse(reader[5].ToString());
                        double thanhtien = gia * sl;
                        item.SubItems.Add(thanhtien.ToString());//don gia
                        listView1.Items.Add(item);
                    }
                    reader.Close();
                    helper.Close();

                    //load lên các label
                    lbtenkh1.Text = gh.TenUser;
                    lbngaymua1.Text = gh.NgayDatHang.ToString();
                    lbtinhtrang1.Text = gh.MaTinhTrangGioHang.ToString();

                    lbmagh1.Text = gh.MaGioHang.ToString();
                    lbtongtien1.Text = double.Parse(gh.TongTien.ToString()).ToString();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Lỗi " + ex.Message);
                }//radioButton2
            }
            else if (radioButton2.Checked == true)
            {
                try
                {
                    Datahelper helper = new Datahelper();
                    helper.Open();
                    SqlCommand comman = new SqlCommand();
                    comman.CommandText = "sp_LayChiTietGioHangTheoMaGH_Phantom";
                    comman.CommandType = CommandType.StoredProcedure;
                    SqlParameter bien1 = comman.Parameters.Add("@magh", SqlDbType.Char, 30);
                    bien1.Direction = ParameterDirection.Input;
                    bien1.Value = gh.MaGioHang;
                    comman.Connection = helper.Connection;

                    SqlDataReader reader = comman.ExecuteReader();
                    listView1.Items.Clear();
                    string masp = "";
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem();

                        item.Text = reader[2].ToString();
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[3].ToString());//so luong
                        item.SubItems.Add(reader[5].ToString());//don gia
                        int sl = int.Parse(reader[3].ToString());
                        float gia = float.Parse(reader[5].ToString());
                        double thanhtien = gia * sl;
                        item.SubItems.Add(thanhtien.ToString());//don gia
                        listView1.Items.Add(item);
                    }
                    reader.Close();
                    helper.Close();

                    //load lên các label
                    lbtenkh1.Text = gh.TenUser;
                    lbngaymua1.Text = gh.NgayDatHang.ToString();
                    lbtinhtrang1.Text = gh.MaTinhTrangGioHang.ToString();

                    lbmagh1.Text = gh.MaGioHang.ToString();
                    lbtongtien1.Text = double.Parse(gh.TongTien.ToString()).ToString();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Lỗi " + ex.Message);
                }//radioButton2
            }
        }
    }
}